package com.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

public class WebConstant implements Filter{
	private String basePath;

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest httpRequest = (HttpServletRequest) request;
        String path = httpRequest.getContextPath();
        basePath = httpRequest.getScheme() + "://" + httpRequest.getServerName() + ":"
                + httpRequest.getServerPort() + path + "/";
        request.setAttribute("website", getWebsite());
	    request.setAttribute("tinyWebsite", getTinyWebSite());
	    request.setAttribute("staticWebsite", getStaticWebsite());
	  //  System.out.println(getWebsite().toString());
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
     //   System.out.println("执行过滤器前");
        chain.doFilter(request, response);
     //   System.out.println("执行过滤器后");
	}

	
    public String getStaticWebsite() {
        return basePath + "resource/";
    }

    public String getTinyWebSite() {
        return basePath + "resource/";
    }


    public String getWebsite() {
        return basePath;
    }


	public void destroy() {
		// TODO Auto-generated method stub
		
	}


	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}
}
